package com.glassshop.ui;

import java.util.ArrayList;
import java.util.List;
import com.glassshop.R;
import com.glassshop.adapter.GuideAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 应用程序类：首次引导界面
 * @author king
 * @version 1.0
 * @date 2014-12-17
 */
public class GuideActivity extends Activity implements OnPageChangeListener{
	
	/** 引导图片View*/
	private List<View> views;
	/** 底部小点图片*/
	private ImageView[] dots;
	/**引导适配器*/
	private GuideAdapter guideAdapter;
	private ViewPager vp;
	private int currentIndex = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guide);
		// 初始化页面
		initViews();
		initDots();
	}

	/**
	 * 初始化引导图片
	 */
	private void initViews() {
		// TODO Auto-generated method stub
		LayoutInflater inflater = LayoutInflater.from(this);
		
		views = new ArrayList<View>();
		// 初始化引导图片列表
		views.add(inflater.inflate(R.layout.first, null));
		views.add(inflater.inflate(R.layout.second, null));
		views.add(inflater.inflate(R.layout.third, null));
		
		// 初始化Adapter
		guideAdapter = new GuideAdapter(views, this);
				
		vp = (ViewPager) findViewById(R.id.viewpager);
		vp.setAdapter(guideAdapter);
		// 绑定回调
		vp.setOnPageChangeListener(this);
		vp.setCurrentItem(0);
		
	}

	/**
	 * 初始化底部小点
	 */
	private void initDots() {
		// TODO Auto-generated method stub
		LinearLayout ll = (LinearLayout) findViewById(R.id.ll_guide);

		dots = new ImageView[views.size()];

		// 循环取得小点图片
		for (int i = 0; i < views.size(); i++) {
			dots[i] = (ImageView) ll.getChildAt(i);
			dots[i].setEnabled(true);// 都设为灰色
		}

		currentIndex = 0;
		dots[currentIndex].setEnabled(false);// 设置为白色，即选中状态
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		setCurrentDot(arg0);
	}

	private void setCurrentDot(int position) {
		// TODO Auto-generated method stub
		if (position < 0 || position > views.size() - 1
				|| currentIndex == position) {
			return;
		}

		dots[position].setEnabled(false);
		dots[currentIndex].setEnabled(true);

		currentIndex = position;
	}

}
